Public Class asignarNotasEstudiantes

    Private gest As capaNegocios.Gestor = capaNegocios.Gestor.instanciar
    Private dtRubro As DataTable
    Private dtLista As DataTable
    Private dtListaEvaluado As DataTable
    Private idR As Integer
    Private idGrupo As Integer
    Private idE As Integer
    Private idE_Evaluado As Integer

    Private Sub AsignarNotasEstudiantes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cargarRubros()
    End Sub

    Public Sub cargarDatos(ByVal pidG As Integer)
        Me.idGrupo = pidG
        'MessageBox.Show(idGrupo, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub

    Private Sub cargarRubros()
        Dim i As Integer
        dtRubro = gest.obtenerRubros(Me.idGrupo)
        For i = 0 To dtRubro.Rows.Count - 1
            Me.listaRubros.Items.Add(dtRubro.Rows.Item(i).Item(1) & " ----- " & _
                                     dtRubro.Rows.Item(i).Item(2) & " % ----- " & _
                                     dtRubro.Rows.Item(i).Item(3))
        Next
        If (Me.dtRubro.Rows.Count > 0) Then
            Me.listaRubros.SelectedItem = Me.listaRubros.Items(0)
        End If
    End Sub

    Private Sub cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        Me.Close()
    End Sub

    Private Sub asignarNota(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NewNota.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Dim nota As String = CType(Me.NewNota.Text, String)
                gest.asignarNota(idGrupo, idE_Evaluado, idR, nota)
                'Me.cargarLista()
                Me.cargarListaEvaluado()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub mostrarNotaEvaluada(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listaEvaluado.SelectionChanged
        Dim fila As Integer
        If listaEvaluado.CurrentRow IsNot Nothing Then
            fila = listaEvaluado.CurrentRow.Index
            idE_Evaluado = CInt(listaEvaluado(0, fila).Value)
            Me.NewNota.Text = listaEvaluado(2, fila).Value
        End If
    End Sub

    Private Sub darNota(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextNota.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Dim fila As Integer
                If lista.CurrentRow IsNot Nothing Then
                    fila = lista.CurrentRow.Index
                    idE = CInt(lista("id", fila).Value)
                    'idE = CInt(dtLista.Rows.Item(fila).Item(0))
                    Dim nota As String = CType(Me.TextNota.Text, String)
                    gest.darNota(idGrupo, idE, idR, nota)
                    Me.cargarLista()
                    Me.cargarListaEvaluado()
                End If
                'MessageBox.Show(fila, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub buscarLista(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listaRubros.SelectedIndexChanged
        Me.lista.Refresh()
        Me.cargarLista()
        Me.cargarListaEvaluado()
        If dtLista.Rows.Count > 0 Then
            Me.TextNota.Enabled = True
        Else
            Me.TextNota.Enabled = False
        End If
        If dtListaEvaluado.Rows.Count > 0 Then
            Me.NewNota.Enabled = True
        Else
            Me.NewNota.Enabled = False
        End If
    End Sub

    Private Sub cargarLista()
        Try
            Me.lista.Refresh()
            Dim num As Integer = Me.listaRubros.SelectedIndex
            'MessageBox.Show(num, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            idR = CInt(dtRubro.Rows.Item(num).Item(0))
            'MessageBox.Show(idGrupo & "" & idR, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            dtLista = gest.mostrarLista(Me.idGrupo, idR)
            Me.lista.DataSource = dtLista
            Me.TextNota.Clear()
            If dtLista.Rows.Count > 0 Then
                Me.TextNota.Enabled = True
            Else
                Me.TextNota.Enabled = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cargarListaEvaluado()
        Try
            Me.listaEvaluado.Refresh()
            Dim num As Integer = Me.listaRubros.SelectedIndex
            idR = CInt(dtRubro.Rows.Item(num).Item(0))
            dtListaEvaluado = gest.mostrarListaEvaluada(Me.idGrupo, idR)
            Me.listaEvaluado.DataSource = dtListaEvaluado
            If (dtListaEvaluado.Rows.Count > 0) Then
                Me.eliminarNota.Enabled = True
                Me.NewNota.Enabled = True
            Else
                Me.eliminarNota.Enabled = False
                Me.NewNota.Enabled = False
                Me.NewNota.Clear()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub eliminarNota_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eliminarNota.Click
        Dim fila, idN As Integer
        If listaEvaluado.CurrentRow IsNot Nothing Then
            'MessageBox.Show("Pasa", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If MessageBox.Show("Desea borrar la nota?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) = Windows.Forms.DialogResult.Yes Then
                fila = listaEvaluado.CurrentRow.Index
                idN = CInt(listaEvaluado("idN", fila).Value)
                gest.eliminarNota(Me.idGrupo, Me.idR, idN)
                Me.cargarLista()
                Me.cargarListaEvaluado()
            End If
        End If
    End Sub
End Class